class Administrate < ActiveRecord::Base
	
	# TODO to sort out dependency/destroy for admin/entity/group
	belongs_to :entity
	belongs_to :group, :class_name => 'Group', :foreign_key => 'group_id'
	
	validates :entity_id, :presence => true
	validates :group_id, :presence => true
	
	before_destroy :ensure_group_remains_administrated
	
	def ensure_group_remains_administrated
		if Group.find(self.group_id).admins.count < 2 
			errors.add_to_base "Cannot remove last administrator."
			false
		else
			true
		end
	end
end